feat: add HasItem expectation for collections#681
Conversation
There was a problem hiding this comment.
Pull Request Overview
This pull request introduces a new HasItem expectation for collection verification, allowing users to verify that collections contain elements matching specific criteria at given indices or any index. The implementation includes full support for both generic and non-generic enumerables, including async enumerables, and provides fluent API methods for index targeting.
- Implementation of
HasItemfunctionality for various collection types with predicate and object matching support - Addition of fluent API methods
AtIndex()andAtAnyIndex()for targeted element verification - Comprehensive test coverage across different collection types and scenarios
Reviewed Changes
Copilot reviewed 33 out of 33 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| ThatEnumerable.HasItem.cs | Core implementation of HasItem extension methods for synchronous collections |
| ThatAsyncEnumerable.HasItem.cs | Async enumerable support for HasItem functionality |
| HasItemResult.cs | Result type providing AtIndex/AtAnyIndex fluent methods |
| HasItemObjectResult.cs | Enhanced result type with Equivalent/Using methods for object comparisons |
| CollectionIndexOptions.cs | Index range validation and description logic |
| Various test files | Extensive test coverage for all HasItem scenarios |
Comments suppressed due to low confidence (16)
Tests/aweXpect.Tests/Collections/ThatEnumerable.HasItem.Tests.cs:38
- The test method name 'WhenEnumerableContainsDifferentItemAtGivenIndex_ShouldSucceed' is misleading. Based on the test logic (lines 45-53), this test expects a failure/exception when the predicate doesn't match, so it should be named something like 'WhenEnumerableContainsDifferentItemAtGivenIndex_ShouldFail'.
public async Task WhenEnumerableContainsDifferentItemAtGivenIndex_ShouldSucceed()
Tests/aweXpect.Tests/Collections/ThatEnumerable.HasItem.Tests.cs:68
- The test method name 'WhenEnumerableContainsNoItemAtGivenIndex_ShouldSucceed' is misleading. The test expects a failure/exception (lines 80-88), so it should be named 'WhenEnumerableContainsNoItemAtGivenIndex_ShouldFail'.
public async Task WhenEnumerableContainsNoItemAtGivenIndex_ShouldSucceed()
Tests/aweXpect.Tests/Collections/ThatEnumerable.HasItem.Tests.cs:195
- The test method name 'WhenEnumerableContainsDifferentItemAtGivenIndex_ShouldSucceed' is misleading. The test expects a failure/exception (lines 205-213), so it should be named 'WhenEnumerableContainsDifferentItemAtGivenIndex_ShouldFail'.
public async Task WhenEnumerableContainsDifferentItemAtGivenIndex_ShouldSucceed(
Tests/aweXpect.Tests/Collections/ThatEnumerable.HasItem.Tests.cs:233
- The test method name 'WhenEnumerableContainsNoItemAtGivenIndex_ShouldSucceed' is misleading. The test expects a failure/exception (lines 245-253), so it should be named 'WhenEnumerableContainsNoItemAtGivenIndex_ShouldFail'.
public async Task WhenEnumerableContainsNoItemAtGivenIndex_ShouldSucceed(int expected)
Tests/aweXpect.Tests/Collections/ThatEnumerable.HasItem.ImmutableTests.cs:16
- The test method name 'WhenEnumerableContainsDifferentItemAtGivenIndex_ShouldSucceed' is misleading. The test expects a failure/exception, so it should be named 'WhenEnumerableContainsDifferentItemAtGivenIndex_ShouldFail'.
public async Task WhenEnumerableContainsDifferentItemAtGivenIndex_ShouldSucceed()
Tests/aweXpect.Tests/Collections/ThatEnumerable.HasItem.ImmutableTests.cs:46
- The test method name 'WhenEnumerableContainsNoItemAtGivenIndex_ShouldSucceed' is misleading. The test expects a failure/exception, so it should be named 'WhenEnumerableContainsNoItemAtGivenIndex_ShouldFail'.
public async Task WhenEnumerableContainsNoItemAtGivenIndex_ShouldSucceed()
Tests/aweXpect.Tests/Collections/ThatEnumerable.HasItem.ImmutableTests.cs:112
- The test method name 'WhenEnumerableContainsDifferentItemAtGivenIndex_ShouldSucceed' is misleading. The test expects a failure/exception, so it should be named 'WhenEnumerableContainsDifferentItemAtGivenIndex_ShouldFail'.
public async Task WhenEnumerableContainsDifferentItemAtGivenIndex_ShouldSucceed()
Tests/aweXpect.Tests/Collections/ThatEnumerable.HasItem.ImmutableTests.cs:142
- The test method name 'WhenEnumerableContainsNoItemAtGivenIndex_ShouldSucceed' is misleading. The test expects a failure/exception, so it should be named 'WhenEnumerableContainsNoItemAtGivenIndex_ShouldFail'.
public async Task WhenEnumerableContainsNoItemAtGivenIndex_ShouldSucceed()
Tests/aweXpect.Tests/Collections/ThatEnumerable.HasItem.EnumerableTests.cs:39
- The test method name 'WhenEnumerableContainsDifferentItemAtGivenIndex_ShouldSucceed' is misleading. The test expects a failure/exception, so it should be named 'WhenEnumerableContainsDifferentItemAtGivenIndex_ShouldFail'.
public async Task WhenEnumerableContainsDifferentItemAtGivenIndex_ShouldSucceed()
Tests/aweXpect.Tests/Collections/ThatEnumerable.HasItem.EnumerableTests.cs:69
- The test method name 'WhenEnumerableContainsNoItemAtGivenIndex_ShouldSucceed' is misleading. The test expects a failure/exception, so it should be named 'WhenEnumerableContainsNoItemAtGivenIndex_ShouldFail'.
public async Task WhenEnumerableContainsNoItemAtGivenIndex_ShouldSucceed()
Tests/aweXpect.Tests/Collections/ThatEnumerable.HasItem.EnumerableTests.cs:191
- The test method name 'WhenEnumerableContainsDifferentItemAtGivenIndex_ShouldSucceed' is misleading. The test expects a failure/exception, so it should be named 'WhenEnumerableContainsDifferentItemAtGivenIndex_ShouldFail'.
public async Task WhenEnumerableContainsDifferentItemAtGivenIndex_ShouldSucceed(
Tests/aweXpect.Tests/Collections/ThatEnumerable.HasItem.EnumerableTests.cs:231
- The test method name 'WhenEnumerableContainsNoItemAtGivenIndex_ShouldSucceed' is misleading. The test expects a failure/exception, so it should be named 'WhenEnumerableContainsNoItemAtGivenIndex_ShouldFail'.
public async Task WhenEnumerableContainsNoItemAtGivenIndex_ShouldSucceed(int expected)
Tests/aweXpect.Tests/Collections/ThatAsyncEnumerable.HasItem.Tests.cs:39
- The test method name 'WhenEnumerableContainsDifferentItemAtGivenIndex_ShouldSucceed' is misleading. The test expects a failure/exception, so it should be named 'WhenEnumerableContainsDifferentItemAtGivenIndex_ShouldFail'.
public async Task WhenEnumerableContainsDifferentItemAtGivenIndex_ShouldSucceed()
Tests/aweXpect.Tests/Collections/ThatAsyncEnumerable.HasItem.Tests.cs:69
- The test method name 'WhenEnumerableContainsNoItemAtGivenIndex_ShouldSucceed' is misleading. The test expects a failure/exception, so it should be named 'WhenEnumerableContainsNoItemAtGivenIndex_ShouldFail'.
public async Task WhenEnumerableContainsNoItemAtGivenIndex_ShouldSucceed()
Tests/aweXpect.Tests/Collections/ThatAsyncEnumerable.HasItem.Tests.cs:190
- The test method name 'WhenEnumerableContainsDifferentItemAtGivenIndex_ShouldSucceed' is misleading. The test expects a failure/exception, so it should be named 'WhenEnumerableContainsDifferentItemAtGivenIndex_ShouldFail'.
public async Task WhenEnumerableContainsDifferentItemAtGivenIndex_ShouldSucceed()
Tests/aweXpect.Tests/Collections/ThatAsyncEnumerable.HasItem.Tests.cs:220
- The test method name 'WhenEnumerableContainsNoItemAtGivenIndex_ShouldSucceed' is misleading. The test expects a failure/exception, so it should be named 'WhenEnumerableContainsNoItemAtGivenIndex_ShouldFail'.
public async Task WhenEnumerableContainsNoItemAtGivenIndex_ShouldSucceed()
## Have item at index
You can verify that the collection contains an item that satisfies the expectation on a given index (or any index).
```csharp
IEnumerable<string> values = ["0th item", "1st item", "2nd item", "3rd item"];
await Expect.That(values).HasItem("1st item").AtIndex(1);
await Expect.That(values).HasItem(a => a.StartsWith("2nd")).AtAnyIndex();
```
*Note: The same expectation works also for `IAsyncEnumerable<T>`.*
dc2748d to
15cc4da
Compare
|
|
This is addressed in release v2.20.0. |



Have item at index
You can verify that the collection contains an item that satisfies the expectation on a given index (or any index).
Note: The same expectation works also for
IAsyncEnumerable<T>.